package com.qcby.byc.demo0422.controller;


import com.qcby.byc.demo0422.entity.Jdk8Demo;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: Jdk8Demo
 * @description:
 * @author: lxt
 * @create: 2021-05-21 16:11
 **/
@Slf4j
public class Jdk8DemoController {
    private static List<Jdk8Demo> initList() {
        List<Jdk8Demo> resultList = new ArrayList<>();
        // 建造者模式创建对象
        resultList.add(Jdk8Demo.builder().id(1L).name("小白").area("A").age(20).score(10.1).build());
        resultList.add(Jdk8Demo.builder().id(2L).name("小红").area("A").age(31).score(9.6).build());
        resultList.add(Jdk8Demo.builder().id(3L).name("小黄").area("B").age(16).score(5.9).build());
        resultList.add(Jdk8Demo.builder().id(4L).name("小蓝").area("C").age(25).score(3.3).build());
        // @Accessors(chain = true)
//        Jdk8Demo.builder().build().setArea("").setArea(1);
//        Jdk8Demo jdk8Demo =  Jdk8Demo.builder().id(1L).name("小白").area("A").age(20).score(10.1).build();
//        jdk8Demo.setArea(":122112").setArea("123123");
        return resultList;
    }


    public static void main(String[] args) {
        List<Jdk8Demo> list = initList();
        System.out.println(list);
        // 1.【转list】提取list中对象的某个字段，返回list。用map进行类型转换，用collect(Collectors.toList())返回新的list。
        List<String> nameList = list.stream().map(Jdk8Demo::getName).collect(Collectors.toList());
        System.out.println("nameList:"+nameList);

        // 2.【过滤】对某个字段过滤，用filter。只有符合filter中的bean才会保留下来。
        List<Jdk8Demo> highCoreList = list.stream().filter(bean -> bean.getScore() > 5.0).collect(Collectors.toList());
        System.out.println("highCoreList:"+ highCoreList);
        // =====================================List排序开始===================================================
        // 3.【排序】根据id这个字段返回倒序排序list。如果是a-b，则是升序。如果是b-a，则是倒序。
        List<Jdk8Demo> sortList = list.stream().sorted((a, b) -> b.getAge() - a.getAge()).collect(Collectors.toList());
        System.out.println("Lis排序1-年龄倒序-sortList:"+ sortList);
        Collections.sort(sortList, Comparator.comparing(Jdk8Demo::getAge));
        System.out.println("Lis排序2-年龄升序-sortList:"+ sortList);
        Collections.sort(sortList, Comparator.comparing(Jdk8Demo::getAge).reversed());
        System.out.println("Lis排序3-年龄倒序-sortList:"+ sortList);
        // =====================================List排序结束===================================================
        // =====================================【List转Map】开始===================================================
        // 4.【转map】list转成map，key为id，value为bean。用Collectors.toMap转成map。
        Map<Long, Jdk8Demo> idBeanMap = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, bean -> bean));
        System.out.println("idBeanMap:"+ idBeanMap);
        // 5.【转map】提取list中信息转成map，key为id，value为name。同上。
        Map<Long, String> idNameMap = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, Jdk8Demo::getName));
        System.out.println("idNameMap:" + idNameMap);
                // 1.转map时，key值需要唯一。否则会报错。
                list.add(Jdk8Demo.builder().id(1L).name("白小白").area("A").age(32).score(10.1).build());

                // 有重复,报错:java.lang.IllegalStateException: Duplicate key 小白
//                Map<Long, String> idNameMapNew = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, Jdk8Demo::getName));
//                System.out.println("idNameMapNew："+idNameMapNew);

                // 我们可以用Collectors.toMap的重载方法，传入一个合并函数来决定如何合并相同key的值即可。
                // 我们默认返回list中第一个出现的重复的值
                Map<Long, String> idNameMapNew1 = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, Jdk8Demo::getName, (value1, value2) -> value1));
                System.out.println("idNameMapNew1："+idNameMapNew1);

                // 我们把两个值按照自己的逻辑合并到一起
                Map<Long, String> idMergeNameMapNew = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, Jdk8Demo::getName, (value1, value2) -> value1 + "和" + value2));
                System.out.println("idMergeNameMap："+ idMergeNameMapNew);
        // =====================================【List转Map】结束===================================================
        // =====================================List分组转map  时间轴类似需求===================================================
        // 6.【转map分组】对list中的bean进行分组，key为area，value为list。
        Map<String, List<Jdk8Demo>> areaListMap = list.stream().collect(Collectors.groupingBy(Jdk8Demo::getArea));
        System.out.println("areaListMap:" + areaListMap);
        // =====================================List分组转map  时间轴类似需求===================================================
        // =====================================map排序===================================================
        // 7.【map排序】  ==> idBeanMap
        // 提取list中信息转成map，key为id，value为age。
        Map<Long, Integer> idAgeMap = list.stream().collect(Collectors.toMap(Jdk8Demo::getId, Jdk8Demo::getAge, (value1, value2) -> value1));
        // 按照Map的key进行排序  ==>倒序   reversed()
        Map<Long, Integer> sortedKeyMap = idAgeMap.entrySet().stream()
                .sorted(Map.Entry.<Long, Integer>comparingByKey().reversed())
                .collect(
                        Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue,
                                (oldVal, newVal) -> oldVal,
                                LinkedHashMap::new
                        )
                );
        // 将排序后的Map打印
        System.out.println("Map按key排序倒序："+ sortedKeyMap);
        // 按照Map的values进行排序  ==>倒序   reversed()
        Map<Long, Integer> sortedValueMap = idAgeMap.entrySet().stream()
                .sorted(Map.Entry.<Long, Integer>comparingByValue().reversed())
                .collect(
                        Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue,
                                (oldVal, newVal) -> oldVal,
                                LinkedHashMap::new
                        )
                );
        System.out.println("Map按value排序倒序："+ sortedValueMap);
        // =====================================map排序===================================================
    }

}
